'use strict';
	angular.module('inventory.invJCJRSingle', [])

	.controller('invJCJRSingleCtrl'
			,['$rootScope', '$scope', 'systemService'
			  , 'uServiceCenter' ,'BizData', 'apiService'
			   , f])//控制器结束
	function f($rootScope,$scope, systemService
					   , uServiceCenter, BizData, apiService
					   ) {
		var common = require('common');
		
		var Model = this;
//		$scope.path = $rootScope.path;
//		$scope.createPermission = $rootScope.path + ':create';
		
		//定义页面级别的变量
		var systemCode = $rootScope.winid; //系统代码

		var flowType = 0;
		var billType = 'JCDJ';
		var billTypeName = "借出单";
		var originName = "";

		var autoCode; //是否自动编码
		var revOpay; //入库出库
		var costingCode; //影响成本码
		//var dealObject;
		//var storeObject;
		var storageTypeD ;
		var storageTypeDLabel ;
		var storageType ;
		var storageTypeLabel ;


		/////////////////////////////////////////////////////////////////////////////////////////////////

		var direction;
		var dealType = 0;
//		var departmentNo;
//		var departmentName;
		var employeeNo;
		var employeeName;
		//var targetNo;
		//var targetName;

		var recordNo;
		var maxDetailNo;
		var serialNo;
		
		var storageID;
		var storageNo;
		var storageName;
		var floorNo;
		//var floorNoD;

		var origin;

		var factoryID;
		var factoryNo;
		var factoryName;
		
		var billNo;

		var state;
		var status;
		var audit;
		var orderDate;
		
		var userId;
		var enterpriseNo;
		var enterpriseName;
		
		var taxType;
		var taxTypeLabel;
		var taxRate;
		
		var providerID;
		var providerNo;
		var shortName
		var providerName;
		
		/**
		name:bizData#onNewCreateParam
		description: <b>[回调型事件]</b>业务新增数据创建新增参数事件，可以增加和修改用户自定义的参数
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"param" : {justep.Request.ActionParam} 新增参数对象,
			"defaultValues" : array - 新增行的默认值数组 ，数组的长度决定新增行数，数组中的对象是列的默认值
				defaultValues格式：
					[
						{列名:默认值,...},
						{列名:默认值,...},
						...
					]
		}
		</xmp>	
		@see justep.XData#newData
		@see justep.Request.ActionParam
		*/
		Model.dataMasterNewCreateParam = function(event) {
			event.param = {"billType": billType};
		}
		
		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		 */
		Model.dataMasterBeforeNew = function(event) {

		};
		/**
		 * name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		 * 
		 * @param event
		 *            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		 */
		Model.dataMasterAfterNew = function(event){
			//创建行后赋值
			event.source.setValue('systemCode',systemCode,event.id);
			
			event.source.setValue('storageTypeD', 'dpt', event.id);	
			event.source.setValue('storageTypeDLabel', '部门', event.id);	
			event.source.setValue('storageType', 'chc', event.id);	
			event.source.setValue('storageTypeLabel', '存货仓', event.id);	
			
			
			//产生自动流水号
			//获取初始化参数
			//获取单据参数
			var enterpriseNo = event.source.getValue("enterpriseNo", event.id);
			if(billType && enterpriseNo){
				var options = {};
				options.billType = billType;
				options.ognID = enterpriseNo;
				options.async = false;
				var billParam = systemService.getBillParam(options);
				if (billParam.state == "success"){
					event.source.setValue('dealType',billParam.dealType,event.id);
					event.source.setValue('billTypeName',billParam.billTypeName,event.id);
					event.source.setValue('flowType',billParam.flowType,event.id);
					event.source.setValue('origin', billParam.origin, event.id);	
					event.source.setValue('originName', billParam.originName, event.id);	
					event.source.setValue('revOpay', billParam.revOpay, event.id);	
					event.source.setValue('direction', billParam.direction, event.id);
					event.source.setValue('costingCode', billParam.costingCode, event.id);	
					
					if (billParam.autoCode = "Y"){
						//产生自动流水号
						event.source.setValue('recordNo',billParam.billNo,event.id);
					}
				}else{
					systemService.dialogMsg('请在单据设置中进行初始化。');
				}
			}
			
		}

		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataMasterBeforeDelete = function(event) {
//			alert(event.deleteIDList[0]);
		}
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataMasterAfterDelete = function(event) {
//			alert('删除之后！')
		}

		
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataMasterIndexChanged = function(event) {
			if(event.rowID!=null){
				Model.dataDetail.fMasterID = event.rowID;
	       		Model.dataDetail.refreshData();
			}else{
				Model.dataDetail.clearData();
			}
		}
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
			"selfChanged" : 是否自身变化，false：从data触发的变化,
			"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataMasterDataChanged = function(event) {
			
		}
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataMasterValueChanged = function(event) {
			uServiceCenter.rkckMasterValueChanged(event);

			if (event.column == "billType"){
				
				var dataMaster = event.source;
				var rowID = dataMaster.getID(event.rowIndex);
				var billTypeMaster = dataMaster.getValue("billType", rowID);
				billTypeName = dataMaster.getValue("billTypeName", rowID);
				
				var enterpriseNo = dataMaster.getValue("enterpriseNo", rowID);
				if(billTypeMaster!=undefined && enterpriseNo!=undefined){
					//产生自动流水号
					//获取初始化参数
					//获取单据参数
					var options = {};
					options.billType = billTypeMaster;
					options.ognID = enterpriseNo;
					options.async = false;
					var data = systemService.getBillParam(options);
					
					if (data.state == "failure"){
						systemService.dialogMsg('请在单据设置中进行初始化。');
					}else{
						billTypeName = data.billTypeName;
//						systemCode = data.systemCode;
						flowType = data.flowType;
						autoCode = data.autoCode;
						revOpay = data.revOpay;
						direction = data.direction; //数字
						costingCode = data.costingCode;
						origin = data.origin;
						originName = data.originName;
						billNo = data.billNo;
					
						//创建行后赋值
						dataMaster.setValue('systemCode',systemCode,rowID);
//						dataMaster.setValue('billType',billType,rowID);
//						dataMaster.setValue('billTypeName',billTypeName,rowID);
						dataMaster.setValue('flowType',1,rowID);
						dataMaster.setValue('revOpay', revOpay, rowID);	
						dataMaster.setValue('direction', direction, rowID);
						dataMaster.setValue('costingCode', costingCode, rowID);	
						dataMaster.setValue('origin', origin, rowID);	
						dataMaster.setValue('originName', originName, rowID);	
						
						dataMaster.setValue('storageTypeD', null, rowID);	
						dataMaster.setValue('storageTypeDLabel', null, rowID);	
						dataMaster.setValue('storageType', 'chc', rowID);	
						dataMaster.setValue('storageTypeLabel', '存货仓', rowID);	
						
						
						if (autoCode = "Y"){
							//产生自动流水号
							dataMaster.setValue('recordNo',billNo,rowID);
						}
					}
					
				}
				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('billType',billType,dataDetail.getID(i));
					dataDetail.setValue('billTypeName',billTypeName,dataDetail.getID(i));
				}
			}
			
			if(event.column=='storageTypeD'){
				var dataMaster = event.source;
				var rowID = dataMaster.getID(event.rowIndex);
	     		dataMaster.setValue('providerID', null, rowID);
	     		dataMaster.setValue('providerNo', null, rowID);
	     		dataMaster.setValue('shortName', null, rowID);
	     		dataMaster.setValue('providerName', null, rowID);
	 			
	     		dataMaster.setValue('settleCategory', null, rowID);
	     		dataMaster.setValue('settleCategoryLabel', null, rowID);
	     		dataMaster.setValue('invoiceCategory', null, rowID);
	     		dataMaster.setValue('invoiceCategoryLabel', null, rowID);
	     		dataMaster.setValue('taxType', 0, rowID);
	     		dataMaster.setValue('taxTypeLabel', '0.应税外加', rowID);
	     		dataMaster.setValue('taxRate', 0, rowID);
	     		dataMaster.setValue('depositRate', 1, rowID);
	     		
	     		dataMaster.setValue('fAddress', null, rowID);
	     		dataMaster.setValue('fAddressDetail', null, rowID);
	     		dataMaster.setValue('contacts', null, rowID);
	     		dataMaster.setValue('mobilePhone', null, rowID);
	     		dataMaster.setValue('fPhone', null, rowID);
			}

			if(event.column=='orderDate'){
				var rowID = event.source.getID(event.rowIndex);
				var orderDate = event.source.getValue('orderDate',rowID);
				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('orderDate',orderDate,dataDetail.getID(i));
				}
			}
			if(event.column=='factoryNo'){
				var rowID = event.source.getID(event.rowIndex);
				var factoryID = event.source.getValue('factoryID',rowID);
				var factoryNo = event.source.getValue('factoryNo',rowID);
				var factoryName = event.source.getValue('factoryName',rowID);
				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('factoryID',factoryID,dataDetail.getID(i));
					dataDetail.setValue('factoryNo',factoryNo,dataDetail.getID(i));
					dataDetail.setValue('factoryName',factoryName,dataDetail.getID(i));
				}
			}
			if(event.column=='employeeNo'){
				var rowID = event.source.getID(event.rowIndex);
				var employeeNo = event.source.getValue('employeeNo',rowID);
				var employeeName = event.source.getValue('employeeName',rowID);
				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('employeeNo',employeeNo,dataDetail.getID(i));
					dataDetail.setValue('employeeName',employeeName,dataDetail.getID(i));
				}
			}
			/*
			if(event.column=='providerNo'){
				var rowID = event.source.getID(event.rowIndex);
				var storageTypeD = event.source.getValue('storageTypeD',rowID);
				var storageTypeDLabel = event.source.getValue('storageTypeDLabel',rowID);
				var providerID = event.source.getValue('providerID',rowID);
				var providerNo = event.source.getValue('providerNo',rowID);
				var shortName = event.source.getValue('shortName',rowID);
				var providerName = event.source.getValue('providerName',rowID);
				var dataDetail = Model.dataDetail;
				for(var i=0;i<dataDetail.getCount();i++){
					dataDetail.setValue('storageTypeD',storageTypeD,dataDetail.getID(i));
					dataDetail.setValue('storageTypeDLabel',storageTypeDLabel,dataDetail.getID(i));
					dataDetail.setValue('providerID',providerID,dataDetail.getID(i));
					dataDetail.setValue('providerNo',providerNo,dataDetail.getID(i));
					dataDetail.setValue('shortName',shortName,dataDetail.getID(i));
					dataDetail.setValue('providerName',providerName,dataDetail.getID(i));
				}
			}
			*/
		}
		
		
		Model.dataMasterBeforeSave = function(event) {
//			console.log('dataMasterBeforeSave-->',event);

		};
		
		
		Model.dataMasterAfterSave = function(event) {
			//保存子表
//			Model.dataDetail.saveData();
			
		}
		
		

		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataDetailBeforeDelete = function(event) {
//			alert(event.deleteIDList[0]);
//			event.cancel = true;
		}
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataDetailAfterDelete = function(event) {
			uServiceCenter.rkckDetailAfterDelete(event);
		}
		
		
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataDetailIndexChanged = function(event) {

		}
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
			"selfChanged" : 是否自身变化，false：从data触发的变化,
			"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataDetailDataChanged = function(event) {
			
		}
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataDetailValueChanged = function(event) {
			uServiceCenter.rkckCalculate(event);
			if(event.column=="itemAmount" 
				|| event.column=="priceAmount"
					|| event.column=="orderAmount"
						|| event.column=="orderAmountOld"
							|| event.column=="orderAmountTax"
								|| event.column=="orderAmountTotal"
					){
				var sum = event.source.getSum(event.column);
				var dataMaster = Model.dataMaster;
				dataMaster.setValue(event.column,sum,dataMaster.getCurrentID());
			}
		}
		

		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		 */
		Model.dataDetailBeforeNew = function(event) {
			// 明细数据新增之前
			// 检查主表中的数据是否完整
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			
			state = dataMaster.getValue('state', rowID);
			status = dataMaster.getValue('status', rowID);
			audit = dataMaster.getValue('audit', rowID);
			orderDate = dataMaster.getValue('orderDate', rowID);
			
			//////////////////////////////////////////////////////////////////////
			systemCode = dataMaster.getValue('systemCode', rowID);
			costingCode = dataMaster.getValue('costingCode', rowID);
//			dealObject = dataMaster.getValue('dealObject', rowID);
//			storeObject = dataMaster.getValue('storeObject', rowID);

			storageType = dataMaster.getValue('storageType', rowID);
			storageTypeLabel = dataMaster.getValue('storageTypeLabel', rowID);
			storageID = dataMaster.getValue('storageID', rowID);
			storageNo = dataMaster.getValue('storageNo', rowID);
			storageName = dataMaster.getValue('storageName', rowID);
			floorNo = dataMaster.getValue('floorNo', rowID);
			//////////////////////////////////////////////////////////////////////

			// 判断如果那些为空，则拒绝新增明细数据
			factoryID = dataMaster.getValue('factoryID', rowID);
			factoryNo = dataMaster.getValue('factoryNo', rowID);
			factoryName = dataMaster.getValue('factoryName', rowID);
			if (factoryNo == null || factoryNo == "") {
				// 取消新增操作
//				alert('工厂不能为空！请先选择主表中的工厂，然后再在明细表中新增行。');
				event.cancel = true;
//				systemService.dialogMsg('工厂不能为空！请先选择主表中的工厂，然后再在明细表中新增行。');
     			$rootScope.addAlert('danger','工厂不能为空！请先选择主表中的工厂，然后再在明细表中新增行。');
				
			}	
			storageTypeD = dataMaster.getValue('storageTypeD', rowID);
			storageTypeDLabel = dataMaster.getValue('storageTypeDLabel', rowID);
			// 判断如果那些为空，则拒绝新增明细数据
			providerID = dataMaster.getValue('providerID', rowID);
			providerNo = dataMaster.getValue('providerNo', rowID);
			shortName = dataMaster.getValue('shortName', rowID);
			providerName = dataMaster.getValue('providerName', rowID);
			
			/*
			if (providerNo == null || providerNo == "") {
				// 取消新增操作
//				alert('出入库部门不能为空！请先选择主表中的出入库部门，然后再在明细表中新增行。')
				event.cancel = true;
				systemService.dialogMsg('出入库部门不能为空！请先选择主表中的出入库部门，然后再在明细表中新增行。');
			}
			*/

			billType = dataMaster.getValue('billType', rowID);
			direction = dataMaster.getValue('direction', rowID);
//			departmentNo = dataMaster.getValue('departmentNo', rowID);
//			departmentName = dataMaster.getValue('departmentName', rowID);
			employeeNo = dataMaster.getValue('employeeNo', rowID);
			employeeName = dataMaster.getValue('employeeName', rowID);
//			targetNo = dataMaster.getValue('targetNo', rowID);
//			targetName = dataMaster.getValue('targetName', rowID);

			recordNo = dataMaster.getValue('recordNo', rowID);
			
			//处理从表流水号
			maxDetailNo=dataMaster.getValue('maxDetailNo', rowID);
			maxDetailNo++;
			var detailNo=common.preZeroFill(maxDetailNo, 5);
			serialNo=detailNo;
				
			dealType = dataMaster.getValue('dealType', rowID);
			billTypeName = dataMaster.getValue('billTypeName', rowID);
			
			//////////////////////////////////////////////////////////////////////
			revOpay = dataMaster.getValue('revOpay', rowID);
			//////////////////////////////////////////////////////////////////////
			
			taxType = dataMaster.getValue('taxType', rowID);
			taxTypeLabel = dataMaster.getValue('taxTypeLabel', rowID);
			taxRate = dataMaster.getValue('taxRate', rowID);
			
			userId = dataMaster.getValue('userId', rowID);
			enterpriseNo = dataMaster.getValue('enterpriseNo', rowID);
			enterpriseName = dataMaster.getValue('enterpriseName', rowID);

		};
		/**
		 * name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		 * 
		 * @param event
		 *            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		 */
		Model.dataDetailAfterNew = function(event){
			var dataDetail = event.source;
			var rowID = event.id;

			dataDetail.setValue('state', state, rowID);
			dataDetail.setValue('status', status, rowID);
			dataDetail.setValue('audit', audit, rowID);
			dataDetail.setValue('orderDate', orderDate, rowID);

			dataDetail.setValue('billType', billType, rowID);
			dataDetail.setValue('direction', direction, rowID);
//			dataDetail.setValue('departmentNo', departmentNo, rowID);
//			dataDetail.setValue('departmentName', departmentName, rowID);
			dataDetail.setValue('employeeNo', employeeNo, rowID);
			dataDetail.setValue('employeeName', employeeName, rowID);
//			dataDetail.setValue('targetNo', targetNo, rowID);
//			dataDetail.setValue('targetName', targetName, rowID);	

			dataDetail.setValue('storageTypeD', storageTypeD, rowID);
			dataDetail.setValue('storageTypeDLabel', storageTypeDLabel, rowID);
			dataDetail.setValue('providerID', providerID, rowID);
			dataDetail.setValue('providerNo', providerNo, rowID);
			dataDetail.setValue('shortName', shortName, rowID);
			dataDetail.setValue('providerName', providerName, rowID);
			
			dataDetail.setValue('factoryID', factoryID, rowID);	
			dataDetail.setValue('factoryNo', factoryNo, rowID);	
			dataDetail.setValue('factoryName', factoryName, rowID);	

			dataDetail.setValue('recordNo', recordNo, rowID);	

			//dataDetail.setValue('serialNo', event.id, rowID);
			dataDetail.setValue('serialNo', serialNo, rowID);

			////////////////////////////////////////////////////////////////////////////////////
			dataDetail.setValue('systemCode', systemCode, rowID);
			dataDetail.setValue('costingCode', costingCode, rowID);
//			dataDetail.setValue('dealObject', dealObject, rowID);
//			dataDetail.setValue('storeObject', storeObject, rowID);
			
			dataDetail.setValue('storageType', storageType, rowID);
			dataDetail.setValue('storageTypeLabel', storageTypeLabel, rowID);
			dataDetail.setValue('storageID', storageID, rowID);
			dataDetail.setValue('storageNo', storageNo, rowID);
			dataDetail.setValue('storageName', storageName, rowID);
			dataDetail.setValue('floorNo', floorNo, rowID);
			/////////////////////////////////////////////////////////////////////////////////////////
			
			dataDetail.setValue('dealType', dealType, rowID);
			dataDetail.setValue('billTypeName', billTypeName, rowID);
			
			////////////////////////////////////////////////////////////////////////////////////
			dataDetail.setValue('revOpay', revOpay, rowID);
			/////////////////////////////////////////////////////////////////////////////////////////
			dataDetail.setValue('materialType', 2, rowID);	//间接材料
			dataDetail.setValue('materialTypeLabel', '2.间接材料', rowID);	//间接材料
			dataDetail.setValue('qualityStatus', 2, rowID);	
			dataDetail.setValue('qualityStatusLabel', '2:合格', rowID);
			
			//更新主表计数器（从表最大流水号）
			var dataMaster = Model.dataMaster;
			var masterRowID = dataMaster.getCurrentID();
			dataMaster.setValue('maxDetailNo', maxDetailNo, masterRowID);	
			
			//更新主表中子表记录数
			dataMaster.setValue('detailsCount', dataMaster.getValue('detailsCount', masterRowID) + 1, masterRowID);	

			dataDetail.setValue('taxType', taxType, rowID);
			dataDetail.setValue('taxTypeLabel', taxTypeLabel, rowID);
			dataDetail.setValue('taxRate', taxRate, rowID);

			dataDetail.setValue('userId', userId, rowID);
			dataDetail.setValue('enterpriseNo', enterpriseNo, rowID);
			dataDetail.setValue('enterpriseName', enterpriseName, rowID);

			dataDetail.setValue('fMasterID',dataDetail.fMasterID,rowID);

		}
		
		///////////////////按钮组开始///////////////////////////////////////////////////
		//新建
		Model.add = function(){
			
		     var options = {
			         index : 0,
			         defaultValues : [
			             {
			            	 state:'active'
			            	 ,status:'open'
			            	 ,audit:'active'
			            	 ,taxType:1
			            	 ,taxTypeLabel:'1.应税内含'
			            	 ,taxRate:0
			            	 
			            	 ,itemAmount:0
			            	 ,orderAmount:0
			            	 ,orderAmountOld:0
			            	 ,orderAmountTax:0
			            	 ,orderAmountTotal:0
			            	 
			            	 ,discountRate:1
			            	 ,discountAmount:0
			            	 ,apportionAmount:0
			            	 
			            	 ,pieceQuantity:0
			            	 
			            	 ,invoiceQuantityIng:0
			            	 ,invoiceQuantity:0
			            	 ,invoiceAmount:0
			            	 ,evaluateQuantityIng:0
			            	 ,evaluateQuantity:0
			            	 ,evaluateAmount:0
			            	 ,backItemIng:0
			            	 ,backItemAmount:0
			            	 ,backPiece:0
			            	 ,orderWeight:0
			            	 
			            	 ,depositRate:0
			            	 ,retailFlag:0
			            	 ,entryCost:0
			            	 ,packFlag:0
			            	 ,entryFlag:0
			            	 ,dealType:0

			            	 ,resultStatus:'待审核'
				             ,paymentStatus:'待开票'
				            	 
			            	 ,exchangeRate:0
			            	 ,printTimes:0
			            	 
			            	 ,detailsCount:0
			            	 ,detailsCountState:0
			            	 ,maxDetailNo:0
			            	 
			            	 ,billType: billType
			            	 ,orderDate: ':getCurrentDate()'
				             ,employeeNo: ':getCurrentPsmFID()'
					         ,employeeName: ':getCurrentPsmFName()'
				             ,cUsercode: ':getCurrentPersonCode()'
					         ,cUsername: ':getCurrentPersonName()'
						     ,cDatetime: ':getCurrentDateTime()'
							 ,userId: ':getCurrentPersonID()'
							 ,enterpriseNo: ':getCurrentOgnFID()'
							 ,enterpriseName: ':getCurrentOgnLongName()'
							 ,version: 0

			            }//,
//			             {itemNo:'a2',itemDescribe:'b2'}
			         ]
			     };    

			Model.dataMaster.newData(options);

			if($scope.tabpanel!='single'){
				$scope.tabpanel='single'
			}
		}
		//修改
		Model.edit = function(){
			Model.dataMaster.editData();
			if($scope.tabpanel!='single'){
				$scope.tabpanel='single'
			}
		}
		
		//保存
		Model.save = function() {
			Model.dataMaster.saveData();
		}

		//取消编辑
		Model.saveCancel = function() {
			Model.dataMaster.editCancel();
		}
		
		/*
		//不提示直接提交后台删除当前行
		var data = justp.xbl('mainData');
		var deleteConfirm = data.deleteConfirm;
		var directDeleteMode = data.directDeleteMode;
		try{
		    data.deleteConfirm = false;
		    data.directDeleteMode = true;
		    data.deleteData(data.getID());
		}finally{
		    data.deleteConfirm = deleteConfirm;
		    data.directDeleteMode = directDeleteMode;
		}
		*/
		Model.remove = function() {
			Model.dataMaster.deleteData(Model.dataMaster.getCurrentID());
			Model.dataMaster.refreshData();
		};
		
		//刷新
		Model.refresh = function() {
			Model.dataMaster.refreshData();
		}
		
		
		//输出：在浏览器窗口打开/下载打印（打印和导出）
		Model.printExport = function() {
			var options = {};
			options.path = $rootScope.getServiceName() + $rootScope.path + "/printPDF";
			options.id = Model.dataMaster.getCurrentID();
			if($rootScope.separation==0){
				//整体项目
				systemService.openPrintExportWindow(options); 
			}else{
				//前后端分离
				systemService.downloadExport(options); 
			}
		};
		
		///////////////////按钮组结束///////////////////////////////////////////////////
		///////////////////子表按钮组开始///////////////////////////////////////////////////
		//增加
		Model.addrow = function(){
		     var options = {
			         index : Model.dataDetail.getCount(),
			         defaultValues : [
			             {
			            	 itemPrice:0.00
			            	 ,discountRate:1
			            	 
			            	 ,arriveAmount:0
			            	 ,wasterAmount:0
			            	 ,ullageAmount:0
			            	 ,arriveAmountBack:0
			            	 ,priceAmount:0
			            	 ,itemAmount:0
			            	 ,orderAmount:0
			            	 ,orderAmountOld:0
			            	 ,orderAmountTax:0
			            	 ,orderAmountTotal:0
			            	 
			            	 ,discountAmount:0
			            	 ,apportionAmount:0
			            	 
			            	 ,piecePackage:0
			            	 ,pieceQuantity:0
			            	 
			            	 ,qualityPeriod:0
			            	 
			            	 ,wasterItemIng:0
			            	 ,wasterItemAmount:0
			            	 ,wasterPiece:0
			            	 ,invoiceQuantityIng:0
			            	 ,invoiceQuantity:0
			            	 ,invoiceAmount:0
			            	 ,evaluateQuantityIng:0
			            	 ,evaluateQuantity:0
			            	 ,evaluateAmount:0
			            	 ,backItemIng:0
			            	 ,backItemAmount:0
			            	 ,backPiece:0
			            	 ,workingHourUsed:0
			            	 ,machineHourUsed:0
			            	 ,itemWeight:0
			            	 ,orderWeight:0
			            	 
			            	 ,finishFlag:0
			            	 ,entryCost:0
			            	 ,evaluateFlag:0
			            	 ,wasterFlag:0
			            	 ,frozenFlag:0
			            	 ,entryFlag:0
			            	 ,invoiceFlag:0
			            	 ,dealType:0
			            	 
			            	 ,sttts:0
			            	 ,lv:0
					            	
			            	 ,batchNo:'********************'
				             ,storeNo:'********************'
			            }
//			            ,{itemNo:'a2',itemDescribe:'b2'}
			         ]
			     };    

			Model.dataDetail.newData(options);
		}
		Model.delrow = function() {
			 if(Model.dataDetail.directDeleteMode){
				//直接删除
				 if(Model.dataDetail.deleteData(Model.dataDetail.getCurrentID())==0){
//					 Model.dataDetail.refreshData();
				 }
			 }else{
				//前台移除
				 if(Model.dataDetail.removeData(Model.dataDetail.getCurrentID())==0){
//					 Model.dataDetail.recordState = 'delete';
				 }
			 }
		};
		///////////////////子表按钮组结束///////////////////////////////////////////////////

		//打开对话框（选择工厂）
		Model.triggerFactoryNoClick = function(){
			
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();
     		var factoryID = dataMaster.getValue('factoryID', rowID);
     		if(factoryID!=null && factoryID!=''){
         		dataMaster.setValue('factoryID', null, rowID);
         		dataMaster.setValue('factoryNo', null, rowID);
         		dataMaster.setValue('factoryName', null, rowID);
     			return;
     		}
			
			var options = {};
			options.recordState = 'none';
			options.id = dataMaster.getValue('enterpriseNo',rowID);

			var modalInstance = systemService.openFactoryNoDialog(options);
	        
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
	             try {
	             	for ( var i = 0; i < returnData.length; i++) {
	             		//alert(returnData[i].rowid+'-->'+returnData[i].billTypeName);

	             		dataMaster.setValue('factoryID', returnData[i].fID, rowID);
	             		dataMaster.setValue('factoryNo', returnData[i].factoryNo, rowID);
	             		dataMaster.setValue('factoryName', returnData[i].factoryName, rowID);
	         			
	                 }
	             } finally {
	             	return;
	             }

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
		};

		//选择交易对象
		Model.triggerDealObjectNoClick = function(){
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();
     		var providerID = dataMaster.getValue('providerID', rowID);
     		if(providerID!=null && providerID!=''){
//         		dataMaster.setValue('storageTypeD', null, rowID);
//         		dataMaster.setValue('storageTypeDLabel', null, rowID);
         		dataMaster.setValue('providerID', null, rowID);
         		dataMaster.setValue('providerNo', null, rowID);
         		dataMaster.setValue('shortName', null, rowID);
         		dataMaster.setValue('providerName', null, rowID);
     			
         		dataMaster.setValue('settleCategory', null, rowID);
         		dataMaster.setValue('settleCategoryLabel', null, rowID);
         		dataMaster.setValue('invoiceCategory', null, rowID);
         		dataMaster.setValue('invoiceCategoryLabel', null, rowID);
         		dataMaster.setValue('taxType', 0, rowID);
         		dataMaster.setValue('taxTypeLabel', '0.应税外加', rowID);
         		dataMaster.setValue('taxRate', 0, rowID);
         		dataMaster.setValue('depositRate', 1, rowID);
         		
         		dataMaster.setValue('fAddress', null, rowID);
         		dataMaster.setValue('fAddressDetail', null, rowID);
         		dataMaster.setValue('contacts', null, rowID);
         		dataMaster.setValue('mobilePhone', null, rowID);
         		dataMaster.setValue('fPhone', null, rowID);
     			return;
     		}
     		
     		var storageTypeD = dataMaster.getValue('storageTypeD', rowID);
     		if(storageTypeD=='pro'){
     			//供应商
     			Model.triggerProviderNoClick();
     		}
     		if(storageTypeD=='cus'){
     			//客户
     			Model.triggerCustomerNoClick();
     		}
     		if(storageTypeD=='psn'){
     			//人员
     			Model.triggerPersonOrgNoClick();
     		}
     		if(storageTypeD=='oth'){
     			//其它
     		}
			
		}
		
		//打开对话框（选择供应商）
		Model.triggerProviderNoClick = function(){
			
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();

     		var options = {};
			options.recordState = 'none';
			options.id = dataMaster.getValue('enterpriseNo',rowID);

			var modalInstance = systemService.openProviderNoDialog(options);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
	             try {
	             	for ( var i = 0; i < returnData.length; i++) {
//	             		alert(returnData[i].sID+'-->'+returnData[i].sFName);
//	             		dataMaster.setValue('storageTypeD', returnData[i].storageTypeD, rowID);
//	             		dataMaster.setValue('storageTypeDLabel', returnData[i].storageTypeDLabel, rowID);
	             		dataMaster.setValue('providerID', returnData[i].fID, rowID);
	             		dataMaster.setValue('providerNo', returnData[i].providerNo, rowID);
	             		dataMaster.setValue('shortName', returnData[i].shortName, rowID);
	             		dataMaster.setValue('providerName', returnData[i].providerName, rowID);
	         			
	             		dataMaster.setValue('settleCategory', returnData[i].settleCategory, rowID);
	             		dataMaster.setValue('settleCategoryLabel', returnData[i].settleCategoryLabel, rowID);
	             		dataMaster.setValue('invoiceCategory', returnData[i].invoiceCategory, rowID);
	             		dataMaster.setValue('invoiceCategoryLabel', returnData[i].invoiceCategoryLabel, rowID);
	             		dataMaster.setValue('taxType', returnData[i].taxType, rowID);
	             		dataMaster.setValue('taxTypeLabel', returnData[i].taxTypeLabel, rowID);
	             		dataMaster.setValue('taxRate', returnData[i].taxRate, rowID);
	             		dataMaster.setValue('depositRate', returnData[i].depositRate, rowID);
	         			
	             		dataMaster.setValue('fAddress', returnData[i].fAddress, rowID);
	             		dataMaster.setValue('fAddressDetail', returnData[i].fAddressDetail, rowID);
	             		dataMaster.setValue('contacts', returnData[i].contacts, rowID);
	             		dataMaster.setValue('mobilePhone', returnData[i].mobilePhone, rowID);
	             		dataMaster.setValue('fPhone', returnData[i].fPhone, rowID);
	                 }
	             } finally {
	             	return;
	             }

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
		}
		
		
		//打开对话框（选择客户）
		Model.triggerCustomerNoClick = function(){
			
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();

     		var options = {};
			options.recordState = 'none';
			options.id = dataMaster.getValue('enterpriseNo',rowID);

			var modalInstance = systemService.openCustomerNoDialog(options);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
	             try {
	             	for ( var i = 0; i < returnData.length; i++) {
//	             		alert(returnData[i].sID+'-->'+returnData[i].sFName);
//	             		dataMaster.setValue('storageTypeD', returnData[i].storageTypeD, rowID);
//	             		dataMaster.setValue('storageTypeDLabel', returnData[i].storageTypeDLabel, rowID);
	             		dataMaster.setValue('providerID', returnData[i].fID, rowID);
	             		dataMaster.setValue('providerNo', returnData[i].customerNo, rowID);
	             		dataMaster.setValue('shortName', returnData[i].shortName, rowID);
	             		dataMaster.setValue('providerName', returnData[i].customerName, rowID);
	         			
	             		dataMaster.setValue('settleCategory', returnData[i].settleCategory, rowID);
	             		dataMaster.setValue('settleCategoryLabel', returnData[i].settleCategoryLabel, rowID);
	             		dataMaster.setValue('invoiceCategory', returnData[i].invoiceCategory, rowID);
	             		dataMaster.setValue('invoiceCategoryLabel', returnData[i].invoiceCategoryLabel, rowID);
	             		dataMaster.setValue('taxType', returnData[i].taxType, rowID);
	             		dataMaster.setValue('taxTypeLabel', returnData[i].taxTypeLabel, rowID);
	             		dataMaster.setValue('taxRate', returnData[i].taxRate, rowID);
	             		dataMaster.setValue('depositRate', returnData[i].depositRate, rowID);
	         			
	             		dataMaster.setValue('fAddress', returnData[i].fAddress, rowID);
	             		dataMaster.setValue('fAddressDetail', returnData[i].fAddressDetail, rowID);
	             		dataMaster.setValue('contacts', returnData[i].contacts, rowID);
	             		dataMaster.setValue('mobilePhone', returnData[i].mobilePhone, rowID);
	             		dataMaster.setValue('fPhone', returnData[i].fPhone, rowID);
	                 }
	             } finally {
	             	return;
	             }

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
		}
		


		//打开对话框（选择经办人）
		Model.triggerEmployeeNoClick = function(){
			
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();
     		var employeeNo = dataMaster.getValue('employeeNo', rowID);
     		if(employeeNo!=null && employeeNo!=''){
         		dataMaster.setValue('employeeNo', null, rowID);
         		dataMaster.setValue('employeeName', null, rowID);
     			return;
     		}
			
			
			var options = {};
			options.id = dataMaster.getValue('enterpriseNo',rowID);
			//是否显示人员
			options.psm = true;

			var modalInstance = apiService.openOrgNoDialog(options);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
	             try {
	             	for ( var i = 0; i < returnData.length; i++) {
//	             		alert(returnData[i].sID+'-->'+returnData[i].sFName);
	             		dataMaster.setValue('employeeNo', returnData[i].sFID, rowID);
	             		dataMaster.setValue('employeeName', returnData[i].sFName, rowID);
	         			
	                 }
	             } finally {
	             	return;
	             }

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
		}


		//打开对话框（选择物料）
		Model.grid_itemNoButtonClick = function(){
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			var options = {};
			
			options.recordState = 'none';
			options.id = dataMaster.getValue('enterpriseNo',rowID);

			var modalInstance = systemService.openItemNoDialog(options);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	/*
	         	var returnData = event.data;
	         	//debugger;
	             try {
	             	for ( var i = 0; i < returnData.length; i++) {
	             		//alert(returnData[i].rowid+'-->'+returnData[i].billTypeName);
	             		var dataDetail = Model.dataDetail;
	             		var rowID = dataDetail.getCurrentID();

	         			dataDetail.setValue('origin', returnData[i].billType, rowID);
	         			dataDetail.setValue('originName', returnData[i].billTypeName, rowID);
	         			dataDetail.setValue('originNo', returnData[i].itemNo, rowID);
	         			
	         			dataDetail.setValue('itemID', returnData[i].fID, rowID);
	         			dataDetail.setValue('itemNo', returnData[i].itemNo, rowID);
	         			dataDetail.setValue('itemDescribe', returnData[i].itemName+'/'+returnData[i].rationSize, rowID);
	         			dataDetail.setValue('itemName', returnData[i].itemName, rowID);
	         			dataDetail.setValue('rationSize', returnData[i].rationSize, rowID);
	         			dataDetail.setValue('classCode', returnData[i].classCode, rowID);
	         			dataDetail.setValue('classLabel', returnData[i].classLabel, rowID);
	         			dataDetail.setValue('itemProperty', returnData[i].itemProperty, rowID);
	         			dataDetail.setValue('unitCode', returnData[i].unitCode, rowID);
	         			dataDetail.setValue('unitName', returnData[i].unitName, rowID);
	         			dataDetail.setValue('process', returnData[i].process, rowID);
	         			dataDetail.setValue('processLabel', returnData[i].processLabel, rowID);
	         			dataDetail.setValue('costingType', returnData[i].costingType, rowID);
	         			dataDetail.setValue('qualityType', returnData[i].qualityType, rowID);
	         			dataDetail.setValue('qualityTypeLabel', returnData[i].qualityTypeLabel, rowID);
	         			dataDetail.setValue('qualityPeriod', returnData[i].qualityPeriod, rowID);
	         			dataDetail.setValue('validDays', returnData[i].validDays, rowID);
	         			dataDetail.setValue('checkoutAgainDays', returnData[i].checkoutAgainDays, rowID);
	         			
	         			dataDetail.setValue('storeNo', returnData[i].storeNo, rowID);
	         			dataDetail.setValue('batchNo', returnData[i].batchNo, rowID);

	             		var billType = dataDetail.getValue('billType',rowID);
	             		//设置仓库
	             		if (billType == 'DBDJ' 
	             			|| billType == 'JCDJ'
	                 			|| billType == 'JCGH'
	                     			|| billType == 'JRDJ'
	                         			|| billType == 'JRGH'
	                             			|| billType == 'BFDJ'
	             			){
	             			dataDetail.setValue('storageTypeD', returnData[i].storageType, rowID);
	             			dataDetail.setValue('storageTypeDLabel', returnData[i].storageTypeLabel, rowID);
	             			dataDetail.setValue('providerID', returnData[i].storageID, rowID);
	             			dataDetail.setValue('providerNo', returnData[i].storageNo, rowID);
	             			dataDetail.setValue('shortName', returnData[i].storageName, rowID);
	             			dataDetail.setValue('providerName', returnData[i].floorNo, rowID);

	             		}else{
	             			dataDetail.setValue('storageType', returnData[i].storageType, rowID);
	             			dataDetail.setValue('storageTypeLabel', returnData[i].storageTypeLabel, rowID);
	             			dataDetail.setValue('storageID', returnData[i].storageID, rowID);
	             			dataDetail.setValue('storageNo', returnData[i].storageNo, rowID);
	             			dataDetail.setValue('storageName', returnData[i].storageName, rowID);
	             			dataDetail.setValue('floorNo', returnData[i].floorNo, rowID);
	            			
	             		}
	             			
	             		//设置单价
	             		var itemPrice;
	             		if (billType == 'XSCK' 
	             			|| billType == 'XSRK'
	                 			|| billType == 'XSDD'
	                     			|| billType == 'XSYC'
	                     				
	                         			|| billType == 'WXFL'
	                             			|| billType == 'XCFL'
	                             				|| billType == 'WXBJ'
	             				){
	             			dataDetail.setValue('itemPrice', returnData[i].salePrice, rowID);
	             		}else{
	             			dataDetail.setValue('itemPrice', returnData[i].itemPrice, rowID);
	             		}
	             		
	             		//设置数量
	                		dataDetail.setValue('itemAmount', 1, rowID);
	                 }
	             } finally {
	             	return;
	             }
	             */
	        	
	         	var simpleRow = event.data[0];
	        	systemService.itemSelectSingle(simpleRow,Model.dataDetail);

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
		}
		
		//打开多选对话框（选择物料）
		Model.addrows = function(){
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			var options = {};
			
			options.recordState = 'none';
			options.id = dataMaster.getValue('enterpriseNo',rowID);
			var modalInstance = systemService.openItemNosDialog(options);
			
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
				var returnData = event.data;
		    	for ( var i = 0; i < returnData.length; i++) {
		    		//新增行
		    		Model.addrow();
		    		var simpleRow = returnData[i];
		        	systemService.itemSelectSingle(simpleRow,Model.dataDetail);
		    	}
	        	
	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
			
		}
		
		//打开对话框（选择仓库）移出地点
		Model.grid_providerNoButtonClick = function(){


     		var dataDetail = Model.dataDetail;
     		var rowID = dataDetail.getCurrentID();
     		
			var options = {};
			options.recordState = 'none';
			options.id = dataDetail.getValue('enterpriseNo',rowID);
			options.factoryID = dataDetail.getValue("factoryID",rowID);
			
			var modalInstance = systemService.openStorageNoDialog(options);

	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
     			dataDetail.setValue('storageTypeD', returnData[0].storageType, rowID);
     			dataDetail.setValue('storageTypeDLabel', returnData[0].storageTypeLabel, rowID);
     			dataDetail.setValue('providerID', returnData[0].fID, rowID);
     			dataDetail.setValue('providerNo', returnData[0].storageNo, rowID);
     			dataDetail.setValue('shortName', returnData[0].storageName, rowID);
     			dataDetail.setValue('providerName', returnData[0].storageFullName, rowID);
     			dataDetail.setValue('procedureStepD', returnData[0].allowed, rowID);

	        }, function (reason) {  
	            console.info('用户取消了操作 at: ' + new Date());  
	        });  
			
		};
		
		//打开对话框（选择仓库）
		Model.triggerStorageNoClick = function(){
     		var dataMaster = Model.dataMaster;
     		var rowID = dataMaster.getCurrentID();
     		var storageID = dataMaster.getValue('storageID', rowID);
     		if(storageID!=null && storageID!=''){
         		dataMaster.setValue('storageType', null, rowID);
         		dataMaster.setValue('storageTypeLabel', null, rowID);
         		dataMaster.setValue('storageID', null, rowID);
         		dataMaster.setValue('storageNo', null, rowID);
         		dataMaster.setValue('storageName', null, rowID);
         		dataMaster.setValue('floorNo', null, rowID);
     			return;
     		}
     		
			var options = {};
			options.id = dataMaster.getValue('enterpriseNo',rowID);
			options.factoryID = dataMaster.getValue("factoryID",rowID);
			
			var modalInstance = systemService.openStorageListDialog(options);

	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
         		dataMaster.setValue('factoryID', returnData[0].factoryID, rowID);
         		dataMaster.setValue('factoryNo', returnData[0].factoryNo, rowID);
         		dataMaster.setValue('factoryName', returnData[0].factoryName, rowID);

	     		dataMaster.setValue('storageType', returnData[0].storageType, rowID);
	     		dataMaster.setValue('storageTypeLabel', returnData[0].storageTypeLabel, rowID);
	     		dataMaster.setValue('storageID', returnData[0].fID, rowID);
	     		dataMaster.setValue('storageNo', returnData[0].storageNo, rowID);
	     		dataMaster.setValue('storageName', returnData[0].storageName, rowID);
	     		dataMaster.setValue('floorNo', returnData[0].storageFullName, rowID);

	        }, function (reason) {  
	            console.info('用户取消了操作 at: ' + new Date());  
	        });  
			
		};
		
		//打开对话框（选择仓库）
		Model.grid_floorNoButtonClick = function(){

     		var storageID = Model.dataMaster.getValue('storageID', Model.dataMaster.getCurrentID());

     		var dataDetail = Model.dataDetail;
     		var rowID = dataDetail.getCurrentID();
     		
			var options = {};
			options.recordState = 'none';
			options.id = dataDetail.getValue('enterpriseNo',rowID);
			options.factoryID = dataDetail.getValue("factoryID",rowID);
			options.storageID = storageID;
			
			var modalInstance = systemService.openStorageNoDialog(options);

	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;
	         	//debugger;
     			dataDetail.setValue('storageType', returnData[0].storageType, rowID);
     			dataDetail.setValue('storageTypeLabel', returnData[0].storageTypeLabel, rowID);
     			dataDetail.setValue('storageID', returnData[0].fID, rowID);
     			dataDetail.setValue('storageNo', returnData[0].storageNo, rowID);
     			dataDetail.setValue('storageName', returnData[0].storageName, rowID);
     			dataDetail.setValue('floorNo', returnData[0].storageFullName, rowID);
     			dataDetail.setValue('procedureStep', returnData[0].allowed, rowID);

	        }, function (reason) {  
	            console.info('用户取消了操作 at: ' + new Date());  
	        });  
			
		};
		
		
		
		/////////////////////////////////////////////////////////////////////
		Model.triggerConfirmClick = function(event){
			//提交之前
			var options = new Object();
			options.bizData = Model.dataDetail;
			if(systemService.rkckConfirmBefore(options)==false){
				return;
			}
			// 提交单据
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invJCJR/single/confirm';
			systemService.operateBill(data);
		};
		Model.triggerActiveClick = function(event){
			// 撤销提交
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invJCJR/single/active';
			systemService.operateBill(data);
		};
		
		Model.triggerAgreeItClick = function(event){
			// 审核单据
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invJCJR/single/agreeIt';
			systemService.operateBill(data);
		};
		Model.triggerUnAgreeClick = function(event){
			// 撤销审核
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invJCJR/single/unAgree';
			systemService.operateBill(data);
		};
		
		Model.triggerCloseClick = function(event){
			// 关闭单据
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invJCJR/single/close';
			systemService.operateBill(data);
		};
		Model.triggerOpenClick = function(event){
			// 撤销审核
			var data = new Object();
			data.bizData = Model.dataMaster;
			data.url = $rootScope.getServiceName() + '/inventory/invJCJR/single/open';
			systemService.operateBill(data);
		};

		///////////////////////////////////////////////////////////////////////////////
		
		
		//双击事件
		Model.gridListRowDblclick = function(rowIndex,rowID){
			$scope.tabpanel = 'single';
		}
		

		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//		Model.objects = {"dataMaster" : Model.dataMaster,"dataDetail" : Model.dataDetail};
		
		var options = {};
		options.container = Model;
		options.concept = 'StorageRecord';
		options.name = 'dataMaster';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/inventory/invJCJR/single/create';
		options.reader = '/inventory/invJCJR/single/query';
		options.writer = '/inventory/invJCJR/single/save';
		options.deleter = '/inventory/invJCJR/single/delete';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"","relation":""};
		options.directDeleteMode = true;
		options.limit = 15;
		options.orderBy = "recordNo DESC";

		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataMasterIndexChanged;
		options.onBeforeNew = Model.dataMasterBeforeNew;
		options.onAfterNew = Model.dataMasterAfterNew;
		options.onBeforeDelete = Model.dataMasterBeforeDelete;
		options.onAfterDelete = Model.dataMasterAfterDelete;
		options.onValueChanged = Model.dataMasterValueChanged;
		options.onDataChanged = Model.dataMasterDataChanged;
		options.onBeforeSave = Model.dataMasterBeforeSave;
		options.onAfterSave = Model.dataMasterAfterSave;
		options.onNewCreateParam = Model.dataMasterNewCreateParam;
		
		Model.dataMaster = new BizData(options);
//		Model.objects.dataMaster = Model.dataMaster;
		//监视对象中的所有变动
		Model.dataMaster.deregRows = $scope.$watch('Model.dataMaster.rows',Model.dataMaster.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值

		
		
		var options = {};
		options.container = Model;
		options.concept = 'StorageRecordDetail';
		options.name = 'dataDetail';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/inventory/invJCJR/single/createDetail';
		options.reader = '/inventory/invJCJR/single/queryDetail';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"dataMaster","relation":"fMasterID"};
		options.directDeleteMode = false;
		options.orderBy = 'serialNo ASC';
		

		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataDetailIndexChanged;
		options.onBeforeNew = Model.dataDetailBeforeNew;
		options.onAfterNew = Model.dataDetailAfterNew;
		options.onBeforeDelete = Model.dataDetailBeforeDelete;
		options.onAfterDelete = Model.dataDetailAfterDelete;
		options.onValueChanged = Model.dataDetailValueChanged;
		options.onDataChanged = Model.dataDetailDataChanged;
//		options.rowsWatcherFn = Model.rowsWatcherFn;
		
		Model.dataDetail = new BizData(options);
//		Model.objects.dataDetail = Model.dataDetail;
		//监视对象中的所有变动
		Model.dataDetail.deregRows = $scope.$watch('Model.dataDetail.rows',Model.dataDetail.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		//增加过滤器，只显示本人及本公司的数据
		Model.dataMaster.filter = "(StorageRecord.billType='JCDJ' OR StorageRecord.billType='JRDJ')" 
			;
		
		Model.dataMaster.refreshData();
		
		$scope.tabpanel = 'list';
		
	    return Model;
	}	
	
